/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.<br/>
 * 项目名称 : 中科软支付平台
 * 创建日期 : 2016年11月23日
 * 修改历史 : 
 *     1. [2016年11月23日]创建文件 by liuyujia
 */
package com.sinosoft.sinopay.service.customer.biz.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.sinocloud.platform.base.SinoBaseService;
import com.sinosoft.sinocloud.platform.common.util.DateUtil;
import com.sinosoft.sinopay.api.common.dto.pay.PayAccountInfoDTO;
import com.sinosoft.sinopay.service.customer.biz.service.CustomerBankAcountCoreService;
import com.sinosoft.sinopay.service.customer.generated.mapper.PayCustomerBankcardMapper;
import com.sinosoft.sinopay.service.customer.generated.model.PayCustomerBankcard;
import com.sinosoft.sinopay.service.customer.generated.model.PayCustomerBankcardExample;

/**  
 * 【客户银行账号信息服务接口实现类】
 * @author liuyujia
 * @date 2016年11月23日 下午5:26:50 
 * @version V1.0  
 */
@Service
public class CustomerBankAcountCoreServiceImpl extends SinoBaseService implements CustomerBankAcountCoreService{

	@Autowired
	private PayCustomerBankcardMapper payCustomerBankcardMapper;
	
	@Override
	public void addNewBankAcountForCustomer(Integer customerId,
			PayAccountInfoDTO accountInfo) {
			Date current = DateUtil.getCurrentDate();
			PayCustomerBankcard bCustomer = new PayCustomerBankcard();
			bCustomer.setCustomerId(customerId);
			bCustomer.setAccountNo(accountInfo.getAccountNo()); //银行卡号
			bCustomer.setBankCode(accountInfo.getBankCode()); //银行编码
			bCustomer.setBankName(accountInfo.getBankName()); //银行名称
			bCustomer.setCertType("1"); //卡折标记  1-卡 0-折
			bCustomer.setMobile(accountInfo.getMobile()); //银行卡手机号
			bCustomer.setPayIndex(accountInfo.getPayIndex()); //支付顺序
			bCustomer.setFastPayFlag(accountInfo.getEasyPayFlag()); //快捷支付标记
			bCustomer.setCheckFlag("N"); //验证标记
			bCustomer.setCreateDate(current);
			payCustomerBankcardMapper.insert(bCustomer);
	}

	@Override
	public void modifyBankAcountForCustomer(Integer customerId,
			PayAccountInfoDTO accountInfo) {
		PayCustomerBankcardExample bCustomerExam = new PayCustomerBankcardExample();
		bCustomerExam.createCriteria().andCustomerIdEqualTo(customerId);
		List<PayCustomerBankcard> bankResult = payCustomerBankcardMapper.selectByExample(bCustomerExam);
		
		Date current = DateUtil.getCurrentDate();
		PayCustomerBankcard bCustomer = new PayCustomerBankcard();
		bCustomer.setSid(bankResult.get(0).getSid());
		bCustomer.setCustomerId(customerId);
		bCustomer.setAccountNo(accountInfo.getAccountNo()); //银行卡号
		bCustomer.setBankCode(accountInfo.getBankCode()); //银行编码
		bCustomer.setBankName(accountInfo.getBankName()); //银行名称
		bCustomer.setCertType("1"); //卡折标记
		bCustomer.setMobile(accountInfo.getMobile()); //银行卡手机号
		bCustomer.setPayIndex(accountInfo.getPayIndex()); //支付顺序
		bCustomer.setFastPayFlag(accountInfo.getEasyPayFlag()); //快捷支付标记
		bCustomer.setCheckFlag("Y"); //验证标记
		bCustomer.setModifyDate(current);
		payCustomerBankcardMapper.updateByPrimaryKeySelective(bCustomer);
	}

	@Override
	public Integer checkCustomerBankExist(Integer customerId,
			PayAccountInfoDTO accountInfo ) {
		//判断客户账户信息是否存在
		PayCustomerBankcardExample bCustomerExam = new PayCustomerBankcardExample();
		bCustomerExam.createCriteria().andCustomerIdEqualTo(customerId);
		List<PayCustomerBankcard> bankResult = payCustomerBankcardMapper.selectByExample(bCustomerExam);
		if(bankResult!=null && bankResult.size()!=0){
//			if(accountInfo.getAccountNo()!=null && bankResult.get(0).getAccountNo()!=null 
//					&& accountInfo.getAccountNo().equals(bankResult.get(0).getAccountNo())){
//				return 1;//客户存在，更新
//			}else{
//				return 0;//新增
//			}
			return 1;
		}else{
			return 0;//新增标记
		}
	}

}
